package com.css.zfzx.fwsjcj.modules.statistics.fw.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.fwsjcj.modules.statistics.fw.service.FwAreaBuildingService;
import com.css.zfzx.fwsjcj.modules.statistics.fw.service.FwDivisionAreaBuildingService;
import com.css.zfzx.fwsjcj.modules.statistics.fw.vo.ProvinceParams;
import com.css.zfzx.fwsjcj.modules.statistics.fw.vo.SelectChinaAndProvinceVo;
import com.css.zfzx.fwsjcj.modules.statistics.fw.vo.SelectStructureVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/fw")
@Slf4j
public class FwAreaBuildingController {

    @Autowired
    private FwAreaBuildingService fwAreaBuildingService;
    @Autowired
    private FwDivisionAreaBuildingService fwDivisionAreaBuildingService;

    /**
     * 定时任务 远程调用
     * 统计全国面积 抽样率 全国栋数 抽样率
     * 统计各个省面积 抽样率 栋数 抽样率
     *统计各个市面积 抽样率 栋数 抽样率
     * @param response
     */
    @GetMapping("/areaAndBuilding")
    public RestResponse getAreaBuilding(HttpServletResponse response) {
        RestResponse restResponse = null;
        try {
            JSONObject jsonObject = new JSONObject();
            Date date = new Date();
            String res = fwAreaBuildingService.getAreaBuilding(date);
            String rest = fwDivisionAreaBuildingService.getDivisionAreaBuilding(date);
            jsonObject.put("全国及省",res);
            jsonObject.put("省市",rest);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 查询地图中全国的相关数据
     * @return
     */
    @GetMapping("/chinaData")
    public RestResponse getChinaData() {
        RestResponse restResponse = null;
        try {
            List<SelectChinaAndProvinceVo> beforeDayData = fwAreaBuildingService.getChinaData();
            restResponse = RestResponse.succeed(beforeDayData);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 查询地图中结构类型的相关数据
     *
     * @return
     */
    @GetMapping("/structureData")
    public RestResponse getStructureData() {
        RestResponse restResponse = null;
        try {
            List<SelectStructureVo> structureData = fwAreaBuildingService.getStructureData();
            restResponse = RestResponse.succeed(structureData);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 查询省的相关数据
     * @return
     */
    @GetMapping("/provinceData")
    public RestResponse getProvinceData() {
        RestResponse restResponse = null;
        try {
            List<SelectChinaAndProvinceVo> beforeDayData = fwAreaBuildingService.getProvinceData();
            restResponse = RestResponse.succeed(beforeDayData);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 查询地图板块中省的数据
     * @return
     */
    @GetMapping("/chinaProvinceData")
    public RestResponse getChinaProvinceData() {
        RestResponse restResponse = null;
        try {
            List<SelectChinaAndProvinceVo> beforeDayData = fwAreaBuildingService.getChinaProvinceData();
            restResponse = RestResponse.succeed(beforeDayData);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 查询省详情
     *
     * @return
     */
    @GetMapping("/queryFwData")
    public RestResponse queryFwData() {
        RestResponse restResponse = null;
        try {
            List<ProvinceParams> list = fwAreaBuildingService.queryProvinceFwData();
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 根据各省更新相关数据
     * @param fwParams
     * @return
     */
    @PostMapping("/updateDataByDistrict")
    public RestResponse updateDataByDistrict(@RequestBody ProvinceParams fwParams) {
        RestResponse restResponse = null;
        try {
            String res = fwAreaBuildingService.updateDataByDistrict(fwParams);
            restResponse = RestResponse.succeed(res);
        } catch (Exception e) {
            String errormessage = "更新失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 上传附件，导入数据
     *
     * @return
     */
    @PostMapping("/uploadFile")
    public RestResponse importFwData(@RequestParam("file") MultipartFile file) {
        RestResponse restResponse = null;
        try {
            fwAreaBuildingService.importExcel(file);
            restResponse = RestResponse.succeed("文件上传成功");
        } catch (Exception e) {
            String mess = e.getMessage();
            System.err.println("mess:" + mess);
            mess = mess.substring(mess.indexOf("=") + 1, mess.length() - 1);
            restResponse = RestResponse.fail(mess);
        }
        return restResponse;
    }



}
